Are `return` and `break` useless inside a Ruby block when used as a callback?
Posted
by
Skilldrick
on Stack Overflow
See other posts from Stack Overflow
or by Skilldrick
Published on 2011-01-15T21:18:04Z
Indexed on
2011/01/15
21:53 UTC
Read the original article
Hit count: 171
In Rails, blocks can be used as callbacks, e.g.:
class User < ActiveRecord::Base
validates_presence_of :login, :email
before_create {|user| user.name = user.login.capitalize
if user.name.blank?}
end
When a block is used like this, is there any use for break
and return
? I'm asking because normally in a block, break
will break out of the loop, and return
will return from the enclosing method. But in a callback context, I can't get my head round what that means.
The Ruby Programming Language suggests that return
could cause a LocalJumpError
but I haven't been able to reproduce this in a Rails callback.
Edit: with the following code I'd expect a LocalJumpError
, but all the return
does is stop the rest of the callback executing.
class User < ActiveRecord::Base
validates_presence_of :login, :email
before_create do |user|
return
user.name = user.login.capitalize
end
© Stack Overflow or respective owner